function [Yp] = mcdsvm(X,Y,C)
[n,d] = size(X);
l = size(Y,1);

cls = unique(Y);
c = size(cls,1);
np =c*(c-1)/2;

sz = zeros(c,1);
idx = cell(c,1);
for i = 1:c
    idx{i}=find(Y==cls(i));
    sz(i)=size(idx{i},1);
end

w = zeros(d,np);
b = zeros(1,np);

ct = 1;
for i = 1:c
    for j = i+1:c
        disp(['Training pair ',int2str(i),' vs ',int2str(j)]);
        ix = [idx{i};idx{j}];
        Xt = X(ix,:);
        Yt = [ones(sz(i),1);-ones(sz(j),1)];
        if (nargin == 2)
            [w(:,ct),b(ct)] = dsvm(Xt,Yt);
        else
            [w(:,ct),b(ct)] = dsvm(Xt,Yt,C);
        end
        ct = ct+1;
    end
end

VM = X*w+repmat(b,n,1);
vot = zeros(n,c);

ct = 1;
for i = 1:c
    for j = i+1:c
        vot(:,i) = vot(:,i)+(VM(:,ct)>0);
        vot(:,j) = vot(:,j)+(VM(:,ct)<0);
        ct=ct+1;
    end
end
[~,Yp] = max(vot,[],2);
Yp = Yp-1;
end